﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using SuperSocket.SocketBase;

namespace SuperSocketDemo_ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var appServer = new AppServer();
            //var testServer=new TestServer();
            //服务器端口  
            int port = 2000;
            string ip = "127.0.0.1";
            //IPAddress ipAddress=IPAddress.Parse(ip);
            //IPEndPoint ipEndPoint=new IPEndPoint(ipAddress,port);
            //新连接事件  
            appServer.NewSessionConnected += new SessionHandler<AppSession>(appServer_NewSessionConnected);

            //收到消息事件  
            //appServer.NewRequestReceived += new RequestHandler<AppSession, StringRequestInfo>(appServer_SessionClosed);
            appServer.NewRequestReceived += appServer_NewRequestReceived;
            //连接断开事件  
            appServer.SessionClosed += new SessionHandler<AppSession, CloseReason>(appServer_SessionClosed);
            //appServer.SessionClosed += appServer_SessionClosed;
            //appServer.NewRequestReceived += appServer_NewRequestReceived;
            //Setup the appServer  
            //设置服务监听端口  
            if (!appServer.Setup(ip, port))
            {
                Console.WriteLine("端口设置失败!");
                Console.ReadKey();
                return;
            }
            


            Console.WriteLine();

            //Try to start the appServer  
            if (!appServer.Start())
            {
                Console.WriteLine("Failed to start!");
                Console.ReadKey();
                return;
            }

            Console.WriteLine("The server started successfully, press key 'q' to stop it!");

            while (Console.ReadKey().KeyChar != 'q')
            {
                Console.WriteLine();
                continue;
            }
            Console.WriteLine();
            //Stop the appServer  
            appServer.Stop();

            //Console.WriteLine("The server was stopped!");
            Console.WriteLine("服务已停止，按任意键退出!");
            Console.ReadKey();
        }
        static void appServer_NewRequestReceived(AppSession session, SuperSocket.SocketBase.Protocol.StringRequestInfo requestInfo)
        {
            var key = requestInfo.Key;
            var body = requestInfo.Body;
            switch (key)
            {
                case "1":
                    Console.WriteLine("hello word");
                    break;
                case "2":
                    Console.WriteLine("LOL");
                    break;
                case "3":
                    Console.WriteLine(body);
                    session.Send(body);
                    break;
            }
        }


        static void appServer_SessionClosed(AppSession session, CloseReason value)
        {
            //throw new NotImplementedException();
            session.Close(value);
        }


        static void appServer_NewSessionConnected(AppSession session)
        {
            Console.WriteLine("Session:" + session.RemoteEndPoint + " connect");
        }
    }
}
